package com.kaikeba.controller;

import com.kaikeba.bean.BootstrapTableCourier;
import com.kaikeba.bean.Courier;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.ResultData;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierService;
import com.kaikeba.service.UserService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;
import org.jacoco.agent.rt.internal_43f5073.core.internal.flow.IFrame;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CourierController
 * @Description TODO
 * @Author Wuky
 * @Time 2022/1/17 10:26
 * @Version 1.0
 **/
public class CourierController {

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> data = CourierService.console();
        Message msg = new Message();
        if(data.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<Courier> list = CourierService.findAll(true, offset, pageNumber);
        int total = CourierService.console().get(0).get("data1_size");


        List<BootstrapTableCourier> list2 = new ArrayList<>();
        for (Courier c : list) {
            String sfzO = c.getSfz();
            String sfz = sfzO.substring(0,5)+"*********"+sfzO.substring(14);
            String registerTime = DateFormatUtil.format(c.getRegisterTime());
            String loginTime = c.getLoginTime() == null?"未激活":DateFormatUtil.format(c.getLoginTime());
            BootstrapTableCourier bc = new BootstrapTableCourier(c.getId(),c.getUsername(),c.getUserPhone(),sfz,c.getPassword(),c.getPieces(),registerTime,loginTime);
            list2.add(bc);
        }

        ResultData<BootstrapTableCourier> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request,HttpServletResponse response){
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String sfz = request.getParameter("sfz");
        String password = request.getParameter("password");
        Courier courier = new Courier(username, userPhone, sfz, password);
        boolean flag = CourierService.insert(courier);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("快递员录入成功！");
        }else{
            msg.setStatus(-1);
            msg.setResult("快递员录入失败！");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request,HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        Courier c = CourierService.findByUserPhone(userPhone);
        Message msg = new Message();
        if (c==null){
            msg.setStatus(-1);
            msg.setResult("快递员不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(c);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
//        手机号
        String userPhone = request.getParameter("userPhone");
//        新快递员信息
        String username = request.getParameter("username");
        String userPhoneNew = request.getParameter("userPhoneNew");
        String sfz = request.getParameter("sfz");
        String password = request.getParameter("password");
        Courier courierNew = new Courier(username, userPhoneNew, sfz, password);
        boolean flag = CourierService.update(userPhone, courierNew);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        boolean flag = CourierService.delete(userPhone);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

}
